package ge.telasi.tasks.ui.structure;

import ge.telasi.tasks.ejb.TaskFacadeRemote;
import ge.telasi.tasks.model.Structure;
import ge.telasi.tasks.ui.Platform;
import ge.telasi.tasks.ui.comp.CommonDialog;
import ge.telasi.tasks.ui.log.Loggable;
import java.awt.Component;
import javax.swing.JComponent;
import static ge.telasi.tasks.ui.log.LoggerUtils.manageException;

/**
 * @author dimitri
 */
public class StructureDialog extends CommonDialog implements Loggable {

    public StructureDialog(Component parent) {
        super(parent, true);
        initComponents();
        getRootPane().setDefaultButton(btnSave);
        setLogger(logger);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnStructure = new ge.telasi.tasks.ui.structure.StructurePanel();
        btnClose = new javax.swing.JButton();
        btnSave = new javax.swing.JButton();
        logger = new ge.telasi.tasks.ui.log.InlineLogger();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        pnStructure.setLogger(logger);

        btnClose.setText("დახურვა");
        btnClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCloseActionPerformed(evt);
            }
        });

        btnSave.setText("შენახვა");
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(297, Short.MAX_VALUE)
                .addComponent(btnSave)
                .addGap(18, 18, 18)
                .addComponent(btnClose)
                .addContainerGap())
            .addComponent(pnStructure, javax.swing.GroupLayout.DEFAULT_SIZE, 455, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(logger, javax.swing.GroupLayout.DEFAULT_SIZE, 431, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(pnStructure, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(logger, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnClose)
                    .addComponent(btnSave))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
        if (pnStructure.validateForm()) {
            Structure formStructure = pnStructure.getStructure();
            try {
                Platform pl = Platform.getDefault();
                TaskFacadeRemote facade = pl.getFacade();
                if (this.structure == null) { // new
                    this.structure = facade.createStructure(pl.getCredentials(), formStructure);
                    approved = true;
                    dispose();
                } else if (this.structure != null) { // update
                    this.structure = facade.updateStructure(pl.getCredentials(), formStructure);
                    approved = true;
                    dispose();
                }
            } catch (Exception ex) {
                manageException(this, getLogger(), ex);
            }
        }

    }//GEN-LAST:event_btnSaveActionPerformed

    private void btnCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCloseActionPerformed
        approved = false;
        dispose();
    }//GEN-LAST:event_btnCloseActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnClose;
    private javax.swing.JButton btnSave;
    private ge.telasi.tasks.ui.log.InlineLogger logger;
    private ge.telasi.tasks.ui.structure.StructurePanel pnStructure;
    // End of variables declaration//GEN-END:variables

    private boolean approved;
    private Structure structure;

    public boolean isApproved() {
        return approved;
    }

    public JComponent[] allFields() {
        throw new UnsupportedOperationException("Not supported.");
    }

    public Structure getStructure() {
        return structure;
    }

    public void setStructure(Structure structure) {
        this.structure = structure;
        pnStructure.setStructure(structure);
        pnStructure.setParentStructure(null);
        setTitle(structure.getName());
    }

    public Structure getParentStructure() {
        return pnStructure.getParentStructure();
    }

    public void setParentStructure(Structure parent) {
        pnStructure.setParentStructure(parent);
        pnStructure.setStructure(null);
        setTitle("ახალი სტრუქტურული ერთეული");
    }

}
